package com.example.myapplication14_1;

import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    // 核心组件引用
    private VideoView videoView;
    private Button btnPlay, btnStop;

    // 控制器与视频路径
    private MediaController mediaController;
    private Uri videoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化界面控件
        initViews();

        // 设置视频资源路径
        setupVideoUri();

        // 配置视频播放器
        setupVideoView();

        // 设置按钮点击事件
        setupButtonListeners();
    }

    /**
     * 初始化所有 UI 组件
     */
    private void initViews() {
        videoView = findViewById(R.id.videoView);
        btnPlay = findViewById(R.id.btnPlay);
        btnStop = findViewById(R.id.btnStop);
    }

    /**
     * 构建本地 raw 资源的 URI
     */
    private void setupVideoUri() {
        String packageName = getPackageName();
        int resourceId = R.raw.testvideo;
        videoUri = Uri.parse("android.resource://" + packageName + "/" + resourceId);
    }

    /**
     * 配置 VideoView 的播放设置
     */
    private void setupVideoView() {
        videoView.setVideoURI(videoUri);

        // 设置 MediaController
        mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);

        // 视频准备监听器
        videoView.setOnPreparedListener(mp -> {
            mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
            mp.setLooping(true); // 循环播放
        });

        // 可选：设置错误监听
        videoView.setOnErrorListener((mp, what, extra) -> {
            // 错误处理逻辑
            return false;
        });
    }

    /**
     * 设置播放 / 停止按钮的点击事件
     */
    private void setupButtonListeners() {
        btnPlay.setOnClickListener(v -> togglePlayPause());

        btnStop.setOnClickListener(v -> stopAndResetVideo());
    }

    /**
     * 切换播放和暂停状态，并更新按钮文本
     */
    private void togglePlayPause() {
        if (videoView.isPlaying()) {
            videoView.pause();
            btnPlay.setText("播放");
        } else {
            videoView.start();
            btnPlay.setText("暂停");
        }
    }

    /**
     * 停止并重置视频播放
     */
    private void stopAndResetVideo() {
        videoView.stopPlayback();
        videoView.setVideoURI(videoUri); // 重新加载视频
        btnPlay.setText("播放");
    }

    /**
     * 当 Activity 暂停时暂停视频播放
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (videoView != null && videoView.isPlaying()) {
            videoView.pause();
            btnPlay.setText("播放");
        }
    }

    /**
     * 清理资源，防止内存泄漏
     */
    @Override
    protected void onDestroy() {
        if (videoView != null) {
            videoView.stopPlayback();
            videoView.suspend(); // 释放 MediaPlayer 资源
            videoView = null; // 防止内存泄漏
        }
        if (mediaController != null) {
            mediaController = null;
        }
        super.onDestroy();
    }
}